package creational.prototype.deepclone2;

import lombok.Data;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @Description: 消息抽象
 * @Author: lxt
 * @CreateDate: 2019/11/1
 **/
@Data
public abstract class AbstractMessage implements Serializable {
    private String from;
    private String to;
    private Content content;

    public abstract void send();

    public Object deepClone() {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        Object clone = null;
        try {
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            clone = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clone;
    }
}
